<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="130px">

      <el-form-item label="ip" prop="ip">
        <el-input style="width:190px;" v-model="dataForm.ip" placeholder="ip"></el-input>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import whiteListT from '@/api/core/whiteListT'
  import MultiUpload from "@/components/upload/multiUpload"

  let pk = '';
  export default {
    components: {MultiUpload},
    data() {
      return {
        visible: false,

        dataForm: {
          id: 0,
          ip: '',
          createTime: '',

        },
        dataRule: {}
      }
    },
    methods: {
      init(id) {
        pk = id
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/columnwhitelistt/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.whiteListT;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        const pattern = /^(\d{1,3}\.){3}\d{1,3}$/;
        if (!pattern.test(this.dataForm.ip)) {
          this.$message.error("ip格式错误：" + this.dataForm.ip)
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/columnwhitelistt/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'data': this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
